const app = getApp();
let tool = require('../../../utils/tool.js');
Page({
  data: {
    pageName: '闯关报告',
    navH: app.globalData.navHeight,
    windowHeight: 0,
    imageUrl: app.globalData.imageurl,
    minutes: parseInt(app.globalData.taskTwoMinuts / 60),
    accuracy: 0, //正确率
    credit: 0, //学分
    diamond: 0, //易学币
    all: 0, //已学
    err: 0, //错误
    remain: 0, //剩余单词
    times: "", //时长
    rtype: 3,
    miao: '分钟',
    date: '', //学习日期
    isContinueDisabled: false,
  },
  getStorage: function() {
    var that = this;
    try {
      let value = wx.getStorageSync('typeReport')
      if (value) {
        console.log(JSON.parse(value));
        let tmparr = JSON.parse(value);
        let m = 0;
        let s = '分钟';
        /** if(tmparr.times<60){
             s='秒';
             m=tmparr.times;
         }else{
             m=Math.floor(tmparr.times / 60)
         }**/
        m = parseInt(tmparr.times);
        this.setData({
          accuracy: tmparr.accuracy, //正确率
          credit: tmparr.credit, //学分
          diamond: tmparr.diamond, //易学币
          all: tmparr.all, //已学
          err: tmparr.err, //错误
          remain: tmparr.remain, //剩余单词
          times: m, //时长
          rtype: 0,
          miao: s
        })
        //如果复习所剩单词数为0，设置按钮不可点
        if (that.data.remain == 0) {
          that.setData({
            isContinueDisabled: true
          })
        }
      } else {
        wx.redirectTo({
          url: '/pages/review/review',
        })
      }
    } catch (e) {
      console.log(e);
      wx.redirectTo({
        url: '/pages/review/review',
      })
    }
  },
  onLoad: function(options) {
    let that = this;
    let temDataAll = options;
    let rtype1 = temDataAll.type;

    console.log(temDataAll);
    if (rtype1 == 0) {

      this.getStorage();
    } else if (rtype1 == 6) {
      this.setData({
        rtype: 6,
        pageName: '学习报告'
      })
      let ids = temDataAll.id;
      this.getData(ids);
    } else {
      let temData = JSON.parse(temDataAll.data);
      let m = 0;
      let s = '分钟';
      /** if(temData.times<60){
           s='秒';
           m=temData.times;
       }else{
           m=Math.floor(temData.times / 60)
       }**/
      m = parseInt(temData.times)
      this.setData({
        accuracy: temData.accuracy, //正确率
        credit: temData.credit, //学分
        diamond: temData.diamond, //易学币
        all: temData.all, //已学
        err: temData.err, //错误
        remain: temData.remain, //剩余单词
        times: m, //时长
        rtype: rtype1,
        miao: s,
        date: temData.date //学习日期

      })
      //如果复习所剩单词数为0，设置按钮不可点
      if (that.data.remain == 0) {
        that.setData({
          isContinueDisabled: true
        })
      }
    }

    wx.getSystemInfo({
      success: res => {
        this.data.windowHeight = res.windowHeight - this.data.navH,
          this.data.windowWidth = res.windowWidth
      },
      fail(err) {
        console.log(err);
      }
    })
    this.setData({
      windowHeight: this.data.windowHeight
    })
    console.log(this.data.windowHeight)
  },
  getData: function(ids) {
    let that = this;
    let paramArr = {
      access_token: app.globalData.studentInfo.access_token,
      id: ids
    }
    let rs = tool.getSign(paramArr, app.globalData.key); //返回 bodyParam heaer sign
    let sign = rs.sign;
    let apiUrl = app.globalData.apiURL + "message/wordreport?sign=" + sign;
    let classPro = new Promise(
      function(resolve, reject) {
        wx.request({
          url: apiUrl,
          header: rs.header,
          data: rs.bodyParam,
          method: 'post',
          success(res) {
            console.log(res);
            resolve(res);
          }
        })
      }
    );

    let temData = new Object();
    classPro.then(function(res) {
      if (res.data.code === 1) {
        temData = res.data.data;
        console.log(temData);
        let m = 0;
        let s = '分钟';
        /**   if(temData.time<60){
               s='秒';
               m=temData.time;
           }else{
               m=Math.floor(temData.time / 60)
           }**/
        m = temData.time

        that.setData({
          accuracy: temData.accuracy, //正确率
          credit: temData.credit, //学分
          diamond: temData.diamond, //易学币
          all: temData.allwords, //已学
          err: temData.difficult, //错误
          // remain: temData.remain,//剩余单词
          times: m, //时长
          miao: s,
          rtype: 6,
        })

      }
    })
  },


  oneMore: function() {
    let urlname = '../listenWorld?date=';
    if (this.data.rtype == 2) {
      urlname = '../../worldMean/worldMean?date=';
    }
    wx.redirectTo({
      url: urlname + this.data.date
    })
  },
  /**查看错词 */
  viewWrong: function() {
    if (this.data.err == 0) {
      wx.showToast({
        title: '暂无错词哦',
        icon: 'none'
      })
      return;
    }
    wx.navigateTo({
      url: '../lworldWrong/lworldWrong?type=' + this.data.rtype
    })
    // wx.redirectTo({
    //   url: '../lworldWrong/lworldWrong?type=' + this.data.rtype
    // })
  },
  goHome: function() {
    wx.redirectTo({
      url: '/pages/review/review',
    })
  },
  onReady: function() {

  },
  onShow: function() {

  },


  onHide: function() {

  },
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})